<?php
require 'config.php';
if ($_SERVER['REQUEST_METHOD']==='POST'){
    $sql="INSERT INTO notes (title,content,author) VALUES (?,?,?)";
    $stmt=$pdo->prepare($sql);
    $stmt->execute([$_POST['title'],$_POST['content'],$_POST['author']?:'匿名']);
    header('Location: index.php');exit;
}
?>
<!doctype html>
<html lang="zh-CN">
<head>
  <meta charset="utf-8">
  <title>写新记录 - 开源粘贴板</title>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" href="assets/ztb.png">
  <link rel="stylesheet" href="assets/bootstrap.min.css">
  <link rel="stylesheet" href="assets/style.css">
</head>
<body>
<div class="container-fluid">
  <div class="row">
    <nav class="col-md-3 col-lg-2 d-md-block sidebar collapse">
      <div class="text-center py-3"><h5 class="text-white">开源粘贴板</h5></div>
      <ul class="nav flex-column px-2">
        <li class="nav-item"><a class="nav-link" href="index.php">📋 全部记录</a></li>
        <li class="nav-item"><a class="nav-link active" href="add.php">✍️ 写新记录</a></li>
        <li class="nav-item"><a class="nav-link" href="#about" data-bs-toggle="modal">ℹ️ 关于</a></li>
      </ul>
    </nav>

    <main class="col-md-9 ms-sm-auto col-lg-10 px-md-4 main">
      <div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
        <h1 class="h2">写新记录</h1>
        <a href="index.php" class="btn btn-secondary btn-sm">返回列表</a>
      </div>

      <div class="card">
        <div class="card-body">
          <form method="post">
            <div class="mb-3">
              <label class="form-label">标题</label>
              <input type="text" class="form-control" name="title" required>
            </div>
            <div class="mb-3">
              <label class="form-label">内容</label>
              <textarea class="form-control" name="content" rows="6" required></textarea>
            </div>
            <div class="mb-3">
              <label class="form-label">署名</label>
              <input type="text" class="form-control" name="author" placeholder="匿名">
            </div>
            <button type="submit" class="btn btn-primary">保存</button>
          </form>
        </div>
      </div>
    </main>
  </div>
</div>

<div class="modal fade" id="about" tabindex="-1">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title">关于</h5>
        <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
      </div>
      <div class="modal-body">
        开源粘贴板 <br>
        基于 PHP + MySQL，Bootstrap 5 美化<br>
        MIT 许可证，可自由二次开发。
      </div>
    </div>
  </div>
</div>
<script src="assets/bootstrap.bundle.min.js"></script>
</body>
</html>